package com.echo.neocommunity.utils;

import org.apache.catalina.connector.InputBuffer;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
@Component
public class SensitiveFilter {

    private static final Logger logger = LoggerFactory.getLogger(SensitiveFilter.class);
    private static final String REPLACEMENT="***";
    private TrieNode root=new TrieNode();

    @PostConstruct
    public void init(){
        try(
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("sensitive-words.txt");
        BufferedReader reader=new BufferedReader(new InputStreamReader(resourceAsStream));
        ){
            String keyWord=null;
            while((keyWord=reader.readLine())!=null){
                this.addKeyWordTrie(keyWord);
            }
        }catch (IOException e){
            logger.error("加载敏感词文件失败！"+e.getMessage());
        }


    }

    private void addKeyWordTrie(String keyWord) {
        TrieNode tmpNode=this.root;
        for(int i=0;i<keyWord.length();i++){
            Character c=keyWord.charAt(i);
            TrieNode subNode = tmpNode.getTriNode(c);
            if(subNode==null){
                subNode=new TrieNode();
                tmpNode.addTrieNode(c,subNode);
            }
            tmpNode=subNode;
            if(i==keyWord.length()-1){
                tmpNode.setKeyWordEnd(true);
            }
        }
    }

    public boolean isSymbol(Character c){
        return !CharUtils.isAsciiAlphanumeric(c) && (c<0x2e80 || c>0x9fff);
    }

    public String filterText(String text){
        if(StringUtils.isEmpty(text)){
            return null;
        }
        TrieNode tempNode=this.root;
        int starter=0;
        int position=0;
        StringBuilder stringBuilder=new StringBuilder();
        while(position<text.length()){
            char c = text.charAt(position);
            if(isSymbol(c)){
                if(tempNode==this.root){
                    stringBuilder.append(c);
                    starter++;
                }
                position++;
                continue;
            }
            tempNode = tempNode.getTriNode(c);
            if(tempNode==null){
                stringBuilder.append(text.charAt(starter));
                position=++starter;
                tempNode=this.root;
            }else if(tempNode.isKeyWordEnd()){
                stringBuilder.append(this.REPLACEMENT);
                starter=++position;
                tempNode=this.root;
            }else {
                position++;
            }
        }
        stringBuilder.append(text.substring(starter));
        return stringBuilder.toString();
    }


    private class TrieNode{

//        节点是否是末尾
        private boolean isKeyWordEnd=false;
//         节点的子节点
        private Map<Character,TrieNode> map=new HashMap<>();
        public boolean isKeyWordEnd() {
            return isKeyWordEnd;
        }

        public void setKeyWordEnd(boolean keyWordEnd) {
            isKeyWordEnd = keyWordEnd;
        }
        /**
         * 添加子节点
         */
        public void addTrieNode(Character c,TrieNode value){
            this.map.put(c,value);
        }

        /**
         * 获取子节点
         */
        public TrieNode getTriNode(Character c){
            return this.map.get(c);
        }

    }


}
